//
//  BindingCell.swift
//  Pods
//
//  Created by 易先生 on 2022/9/9.
//

import UIKit
import MYModule
import CRPSmartRing
import SDWebImage

class BindingCell: TableViewCell {

	override func makeUI() {
		super.makeUI()

		self.selectionStyle = .default

		contentView.addSubview(deviceImageView)
		contentView.addSubview(stackView)
        contentView.addSubview(isBattryImageView)
    
        contentView.addSubview(titleLabel)
        contentView.addSubview(accessoryImageView)
        
        stackView.addArrangedSubview(sizeLabel)
        stackView.addArrangedSubview(macAddressLabel)

		deviceImageView.snp.makeConstraints { make in
			make.centerY.equalToSuperview()
			make.leading.equalToSuperview()
			make.size.equalTo(CGSize(width: 82, height: 80))
            make.top.equalTo(10.0)
		}
        
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(deviceImageView.snp.trailing).offset(5)
            make.top.equalTo(deviceImageView.snp.top)
        }

		stackView.snp.makeConstraints { make in
            make.leading.equalTo(deviceImageView.snp.trailing).offset(5)
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
			make.trailing.equalToSuperview().offset(-5)
		}
        
        isBattryImageView.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(17)
            make.centerY.equalTo(titleLabel.snp.centerY)
            
        }
        

        accessoryImageView.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-32)
            make.centerY.equalToSuperview()
        }
        
      
	}

	public func bind(to model: BindingModel) {

        titleLabel.text = model.discovery.localName
        macAddressLabel.text = model.discovery.mac
        let hide = model.discovery.hide ?? 0
        isBattryImageView.isHidden = hide == 0
        
    
        if let info = model.discovery.info, let colorType = DeviceInfo.RingColorType(rawValue: info.color), let imageUrl = URL(string: model.imageURL) {
            
            sizeLabel.text = "\(colorType.name) \("firmware_device_size_title".language) \(info.size)"
            
            let placeholderImage = UIImage(named: colorType.imageName)
            deviceImageView.sd_setImage(with: imageUrl, placeholderImage: placeholderImage)
        }
        
	}

	private lazy var deviceImageView: ImageView = {
		let watchImageView = ImageView()
		watchImageView.image = UIImage(named: "ic_ring")
		watchImageView.contentMode = .scaleToFill
		return watchImageView
	}()
    
    private lazy var accessoryImageView: ImageView = {
        
        let accessoryImageView = ImageView()
        accessoryImageView.image = UIImage(named: "ic_next_continue")
        return accessoryImageView
    }()

	private lazy var stackView: UIStackView = {
		let stackView = UIStackView()
		stackView.axis = .vertical
		stackView.distribution = .fill
		stackView.spacing = 5
		stackView.alignment = .leading
		return stackView
	}()

	private lazy var titleLabel: Label = {
		let titleLabel = Label()
		titleLabel.font = Fonts.systemFont(ofSize: 24)
		titleLabel.textColor = UIColor.assist_222222
		return titleLabel
	}()
    
    private lazy var sizeLabel: Label = {
        let sizeLabel = Label()
        sizeLabel.font = Fonts.systemFont(ofSize: 12)
        sizeLabel.textColor = UIColor.assist_A9A9A9
        return sizeLabel
    }()

	private lazy var macAddressLabel: Label = {
		let macAddressLabel = Label()
		macAddressLabel.font = Fonts.systemFont(ofSize: 12)
		macAddressLabel.textColor = UIColor.assist_A9A9A9
		return macAddressLabel
	}()
    
    private lazy var isBattryImageView: ImageView = {
        let imageView = ImageView()
        imageView.image = UIImage(named: "battery")
        imageView.isHidden = true
        return imageView
        
        
    }()

	
}
